package dao.impl;

import bean.Athlete;
import bean.User;
import dao.UserDao;
import util.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
    public UserDaoImpl() {

    }

    /**
     * 用id进行查询
     *
     * @param id
     * @return
     */
    @Override
    public User select(String id) {
        User user = new User();
        try {
            Connection conn = JdbcUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement("select * from user where id=?");
            ps.setString(1, id);
            ResultSet resultSet = ps.executeQuery();

            if (resultSet.next()) {
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                user.setId(id);
                user.setUsername(name);
                user.setPassword(password);
            }

            JdbcUtils.close(conn, ps, resultSet);

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }

    /**
     * 查询所有的
     */
    public ArrayList<User> selectAll() {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ArrayList<User> userList = new ArrayList<>();
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("select * from user ");
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                String name = rs.getString("name");
                String password = rs.getString("password");
                String a_id = rs.getString("a_id");
                user.setId(a_id);
                user.setUsername(name);
                user.setPassword(password);
                userList.add(user);
            }
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }

        return null;
    }

    /**
     * 根据用户名查询
     *
     * @param o    没用到，用来构造重载函数
     * @param name 用户名
     * @return
     */
    @Override
    public User select(Object o, String name) {
        User user = null;
        try {
            Connection conn = JdbcUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement("select * from user where name=?");
            ps.setString(1, name);
            ResultSet resultSet = ps.executeQuery();
            user = new User();
            if (resultSet.next()) {
                String id = resultSet.getString("id");
                String password = resultSet.getString("password");
                user.setId(id);
                user.setUsername(name);
                user.setPassword(password);
            }

            JdbcUtils.close(conn, ps, resultSet);

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public void update(User user) {

        try {
            Connection conn = JdbcUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement("update user set name=?,password=? where id=? ");
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getId());
            ps.execute();
            JdbcUtils.close(conn, ps);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(String id) {
        try {
            Connection conn = JdbcUtils.getConnection();
            PreparedStatement ps = conn.prepareStatement("delete from user where id=?");
            ps.setString(1, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean add(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("insert into user values (?,?,?)");
            ps.setString(1, user.getId());
            ps.setString(2, user.getUsername());
            ps.setString(3, user.getPassword());
            int i = ps.executeUpdate();
            if (i == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return false;
    }


}
